﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        //判断数字是否为素数
        static bool isPrime(int num)
        {
            bool result = true;
            if (num <= 1)//特殊情况排除
            {
                result = false;
            }
            else
            {
                for (int i = 2; i <= num / 2; i++)
                {
                    if (num % i == 0)
                    {
                        result = false;
                        break;
                    }
                }
            }

            return result;
        }

        //获取数字所有因子
        static Queue<int> getFactor(int num)
        {
            Queue<int> factors = new Queue<int>();
            for (int i = 1; i <=num; i++)
            {
                //找到因子
                if (num % i == 0)
                {   //将因子加入队列                  
                    factors.Enqueue(i);  
                }
            }

            return factors;
        }

        static void Main(string[] args)
        {
            //获取用户输入

            int num = 0;
       
            //检测用户输入的合法性
            try
            {
                Console.WriteLine("请输入任意正整数");
                num = Convert.ToInt32(Console.ReadLine());
                if (num <= 0) { throw new Exception("不合法的输入"); }
                Queue<int> factor = getFactor(num);
                Console.WriteLine("该整数的素数因子有：");
                foreach (int n in factor) //输出所有素数因子
                {
                    if (isPrime(n))
                    {
                        Console.Write(n + " ");
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("不合法的输入");
            }
        }
    }
}